﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


namespace DutyWeb
{
    public partial class RichTextBox : System.Web.UI.Page
    {
        SqlHelper sqlHelper = new SqlHelper();
        private static DataTable dtUser;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                bindData(true);
            this.BtSave.Enabled = false;
        }

        /**/
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="refresh"></param>
        private void bindData(bool refresh)
        {

            if (refresh || dtUser == null)
            {
                DataSet ds = sqlHelper.getDs("Select * from ApplyForFinanceItems", CommandType.Text, null, "ApplyForFinanceItems");
                dtUser = ds.Tables[0];
            }

            this.myGridView.DataSource = dtUser.DefaultView;
            this.myGridView.DataBind();
        }

        /**/
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void txtDel_Click(object sender, EventArgs e)
        {
            Guid id = new Guid(((LinkButton)sender).CommandArgument);

            //从数据库中删除
            string Sql;
            Sql = "Delete from ApplyForFinanceItems where ID=" + id;
            sqlHelper.ExecuteSql(Sql, CommandType.Text, null);

            //从内存中删除
            DataRow[] dr = dtUser.Select("ID=" + id);
            if (dr.Length > 0)
                dtUser.Rows.Remove(dr[0]);
            this.bindData(false);
        }

        /**/
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtAdd_Click(object sender, EventArgs e)
        {
            DataRow row = dtUser.NewRow();
            //row["ID"] = (Guid)getMaxIdInTable(dtUser, "ID") + 1;
            //row["BirthDay"] = DateTime.Now;
            dtUser.Rows.Add(row);

            this.myGridView.EditIndex = dtUser.Rows.Count - 1;
            this.bindData(false);
            this.BtSave.Enabled = true;
        }

        /**/
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtSave_Click(object sender, EventArgs e)
        {
            int i = this.myGridView.EditIndex;
            //string userName = ((TextBox)this.myGridView.Rows[i].FindControl("txtUserName")).Text.ToString();
            //string password = ((TextBox)this.myGridView.Rows[i].FindControl("txtPassword")).Text.ToString();
            //string describe = ((TextBox)this.myGridView.Rows[i].FindControl("txtDescribe")).Text.ToString();
            //DateTime birthDay = ((Calendar)this.myGridView.Rows[i].FindControl("txtBirthDay")).SelectedDate;
            decimal MoneyAmount = decimal.Parse(((TextBox)this.myGridView.Rows[i].FindControl("txtMoneyAmount")).Text.ToString());
            string ApplyReason = ((TextBox)this.myGridView.Rows[i].FindControl("txtApplyReason")).Text.ToString();
            DateTime OccuredDate = DateTime.Parse(((TextBox)this.myGridView.Rows[i].FindControl("txtOccuredDate")).Text.ToString());
            string PersonName = ((TextBox)this.myGridView.Rows[i].FindControl("txtPersonName")).Text.ToString();
            int DateOfDuration = int.Parse(((TextBox)this.myGridView.Rows[i].FindControl("txtDateOfDuration")).Text.ToString());
            string Description = ((TextBox)this.myGridView.Rows[i].FindControl("txtDescribe")).Text.ToString();

            int baseUserID = 0;
            string Sql;
            Sql = "Insert Into ApplyForFinanceItems(CreatedBy,ModifiedBy,MoneyAmount,ApplyReason,OccuredDate,PersonName,DateOfDuration,Description) values(@CreatedBy,@ModifiedBy,@MoneyAmount,@ApplyReason,@OccuredDate,@PersonName,@DateOfDuration,@Description)";
            SqlParameter[] Parameters = new SqlParameter[8];
            Parameters[0] = new SqlParameter("@CreatedBy", baseUserID);
            Parameters[1] = new SqlParameter("@ModifiedBy", baseUserID);
            Parameters[2] = new SqlParameter("@MoneyAmount", MoneyAmount);
            Parameters[3] = new SqlParameter("@ApplyReason", ApplyReason);
            Parameters[4] = new SqlParameter("@OccuredDate", OccuredDate);
            Parameters[5] = new SqlParameter("@PersonName", PersonName);
            Parameters[6] = new SqlParameter("@DateOfDuration", DateOfDuration);
            Parameters[7] = new SqlParameter("@Description", Description);

            /*
             cmd.Parameters.AddWithValue("Id",Id);和cmd.Parameters.AddWithValue("@Id",Id);
             */
            sqlHelper.ExecuteSql(Sql, CommandType.Text, Parameters);
            this.myGridView.EditIndex = -1;
            this.bindData(true);
            this.BtSave.Enabled = false;

        }

        /**/
        /// <summary>
        /// 得到指定表中关键字的最大值
        /// </summary>
        /// <param name="table"></param>
        /// <param name="keyID"></param>
        /// <returns></returns>
        private object getMaxIdInTable(DataTable table, string keyID)
        {
            if (table.Rows.Count == 0)
                return 0;
            DataView dv = new DataView();
            dv.Table = table;
            dv.Sort = keyID + " Desc";
            return dv[0][keyID];
        }

        /**/
        /// <summary>
        /// 用户取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.myGridView.EditIndex = -1;
            this.bindData(true);
        }

        /**/
        /// <summary>
        /// 用户更新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = this.myGridView.EditIndex;
            if (dtUser.Rows[i].RowState == DataRowState.Added)
            {
                BtSave_Click(sender, e);
                dtUser.Rows[i].AcceptChanges();
            }
            else
            { //表示修改
                Label labID = this.myGridView.Rows[i].FindControl("LabID") as Label;// Convert.ToInt16(this.myGridView.Rows[i].Cells[1].Text);
                Guid id = new Guid(labID.Text.ToString());
                decimal MoneyAmount = decimal.Parse(((TextBox)this.myGridView.Rows[i].FindControl("txtMoneyAmount")).Text.ToString());
                string ApplyReason = ((TextBox)this.myGridView.Rows[i].FindControl("txtApplyReason")).Text.ToString();
                DateTime OccuredDate = DateTime.Parse(((TextBox)this.myGridView.Rows[i].FindControl("txtOccuredDate")).Text.ToString());
                string PersonName = ((TextBox)this.myGridView.Rows[i].FindControl("txtPersonName")).Text.ToString();
                int DateOfDuration = int.Parse(((TextBox)this.myGridView.Rows[i].FindControl("txtDateOfDuration")).Text.ToString());
                string Description = ((TextBox)this.myGridView.Rows[i].FindControl("txtDescribe")).Text.ToString();

                int baseUserID = 0;

                string Sql;
                Sql = "Update ApplyForFinanceItems Set MoneyAmount=@MoneyAmount,ApplyReason=@ApplyReason,OccuredDate=@OccuredDate,PersonName=@PersonName,DateOfDuration=@DateOfDuration,Description=@Description,ModifiedBy=@ModifiedBy,ModifiedOn=@ModifiedOn where id=@id";

                SqlParameter[] Parameters = new SqlParameter[9];
                Parameters[0] = new SqlParameter("@ModifiedOn", DateTime.Now);
                Parameters[1] = new SqlParameter("@ModifiedBy", baseUserID);
                Parameters[2] = new SqlParameter("@MoneyAmount", MoneyAmount);
                Parameters[3] = new SqlParameter("@ApplyReason", ApplyReason);
                Parameters[4] = new SqlParameter("@OccuredDate", OccuredDate);
                Parameters[5] = new SqlParameter("@PersonName", PersonName);
                Parameters[6] = new SqlParameter("@DateOfDuration", DateOfDuration);
                Parameters[7] = new SqlParameter("@Description", Description);
                Parameters[8] = new SqlParameter("@id", id);

                sqlHelper.ExecuteSql(Sql, CommandType.Text, Parameters);
                this.myGridView.EditIndex = -1;
                this.bindData(true);
                this.BtSave.Enabled = false;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        { }

        /**/
        /// <summary>
        /// 用户删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int selectIndex = e.RowIndex;

            //这里的 Cells[1]　对应的是编号列
            int id = Convert.ToInt16(this.myGridView.Rows[selectIndex].Cells[1].Text);

            string Sql;
            Sql = "Delete from ApplyForFinanceItems where ID=" + id;
            sqlHelper.ExecuteSql(Sql, CommandType.Text, null);

            //从内存中删除
            DataRow[] dr = dtUser.Select("ID=" + id);
            dtUser.Rows.Remove(dr[0]);
            this.bindData(false);
        }

        /**/
        /// <summary>
        /// 用户编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.myGridView.EditIndex = e.NewEditIndex;
            this.bindData(false);
        }

        protected void myGridView_DataBound(object sender, EventArgs e)
        {
            string userName = "";
            foreach (GridViewRow r in this.myGridView.Rows)
            {
                //userName = ((Label)r.FindControl("showUserName")).Text;
                ((LinkButton)r.FindControl("txtDel")).Attributes.Add("onclick", "return checkDel(" + userName + ")");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

        }
    }

}